package com.jeeplus.modules.ap.util;


import com.jeeplus.modules.ap.entity.*;
import com.jeeplus.modules.infor.entity.TakeAddress;
import com.jeeplus.modules.tms.entity.TmsWaybill;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import org.apache.pdfbox.multipdf.PDFMergerUtility;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by DHW on   12/13.
 */
public class FksqPrint {


    public void updatePdfa(PaymentApplication p , List<PaymentApproval>l ,String templateFile, File file) throws Exception {

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        PdfReader reader = new PdfReader(templateFile); // 模版文件目录
        FileOutputStream fos = new FileOutputStream(file);

        PdfStamper ps = new PdfStamper(reader, fos); // 生成的输出流

        BaseFont bf = BaseFont.createFont("c:/WINDOWS/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

        AcroFields s = ps.getAcroFields();

        //主表
        s.setFieldProperty("sqbm", "textfont", bf, null);
        s.setFieldProperty("sqr", "textfont", bf, null);
        s.setFieldProperty("sqrq", "textfont", bf, null);
        s.setFieldProperty("fylx", "textfont", bf, null);
        s.setFieldProperty("sfdj", "textfont", bf, null);
        s.setFieldProperty("fkrq", "textfont", bf, null);
        s.setFieldProperty("yy", "textfont", bf, null);
        s.setFieldProperty("xc", "textfont", bf, null);
        s.setFieldProperty("bz", "textfont", bf, null);
        s.setFieldProperty("je", "textfont", bf, null);
        s.setFieldProperty("dx", "textfont", bf, null);
        s.setFieldProperty("xx", "textfont", bf, null);
        s.setFieldProperty("skr", "textfont", bf, null);
        s.setFieldProperty("khh", "textfont", bf, null);
        s.setFieldProperty("yhzh", "textfont", bf, null);
        s.setFieldProperty("spzt", "textfont", bf, null);
        s.setFieldProperty("wan", "textfont", bf, null);
        s.setFieldProperty("qian", "textfont", bf, null);
        s.setFieldProperty("bai", "textfont", bf, null);
        s.setFieldProperty("shi", "textfont", bf, null);
        s.setFieldProperty("ge", "textfont", bf, null);
        s.setFieldProperty("fplx", "textfont", bf, null);
        s.setFieldProperty("fkdw", "textfont", bf, null);
        s.setFieldProperty("fkfs", "textfont", bf, null);
        s.setFieldProperty("mx1", "textfont", bf, null);
        s.setFieldProperty("mx2", "textfont", bf, null);
        s.setFieldProperty("mx3", "textfont", bf, null);
        s.setFieldProperty("mx4", "textfont", bf, null);
        s.setFieldProperty("mx5", "textfont", bf, null);
        s.setFieldProperty("mx6", "textfont", bf, null);
        s.setFieldProperty("mx7", "textfont", bf, null);
        s.setFieldProperty("mx8", "textfont", bf, null);
        s.setFieldProperty("mx9", "textfont", bf, null);
        s.setFieldProperty("mx10", "textfont", bf, null);
        s.setFieldProperty("mx11", "textfont", bf, null);
        s.setFieldProperty("mx12", "textfont", bf, null);

        //设置值（主表）
        s.setField("sqbm", p.getBm());
        s.setField("sqr", p.getSqr());
        s.setField("sqrq", sdf.format(p.getDate()));
        s.setField("fylx", p.getType());
        s.setField("sfdj", p.getSfdj());
        s.setField("fkrq", sdf.format(p.getFkrq()));
        s.setField("yy", p.getYy());
        s.setField("xc", p.getXc());
        s.setField("bz", p.getBz());
        s.setField("je", p.getJe());
        s.setField("dx", "");
        s.setField("xx", p.getJe());
        s.setField("skr", p.getSkr());
        s.setField("khh", p.getKhyh());
        s.setField("yhzh",p.getYhzh());
        s.setField("spzt", p.getStatus());
        s.setField("fplx", p.getFplx());
        s.setField("fkdw",p.getFkdw());
        s.setField("fkfs", p.getFkfs());
        int w;
        int q;
        int b;
        int sh;
        int g;
        int a;

             a=Integer.parseInt(p.getJe());
             w=a/10000;
             q=a%10000/1000;
            b=a%1000/100;
             sh=a%100/10;
            g=a%10;
            s.setField("wan", getNumber(w));
            s.setField("qian", getNumber(q));
            s.setField("bai", getNumber(b));
            s.setField("shi", getNumber(sh));
            s.setField("ge", getNumber(g));

        for(int i=1;i<=l.size();i++){
            s.setField("mx"+i, fz(l.get(i-1)));
        }
        ps.setFormFlattening(true); // 这句不能少

        ps.close();
        reader.close();
        fos.close();
    }

    public void updatePdfa1(ProcurementApplication  p ,List<ApplicationApproval>l, String templateFile, File file) throws Exception {

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        PdfReader reader = new PdfReader(templateFile); // 模版文件目录
        FileOutputStream fos = new FileOutputStream(file);

        PdfStamper ps = new PdfStamper(reader, fos); // 生成的输出流

        BaseFont bf = BaseFont.createFont("c:/WINDOWS/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

        AcroFields s = ps.getAcroFields();

        //主表
        s.setFieldProperty("bm", "textfont", bf, null);
        s.setFieldProperty("sqr", "textfont", bf, null);
        s.setFieldProperty("sqrq", "textfont", bf, null);
        s.setFieldProperty("zje", "textfont", bf, null);
        s.setFieldProperty("zjehs", "textfont", bf, null);
        s.setFieldProperty("pjlx", "textfont", bf, null);
        s.setFieldProperty("pjlxhs", "textfont", bf, null);
        s.setFieldProperty("lxfs", "textfont", bf, null);
        s.setFieldProperty("cgsm", "textfont", bf, null);
        s.setFieldProperty("ssbm", "textfont", bf, null);
        s.setFieldProperty("ysbm", "textfont", bf, null);
        s.setFieldProperty("spzt", "textfont", bf, null);
        s.setFieldProperty("mc1", "textfont", bf, null);
        s.setFieldProperty("pp1", "textfont", bf, null);
        s.setFieldProperty("gg1", "textfont", bf, null);
        s.setFieldProperty("sl1", "textfont", bf, null);
        s.setFieldProperty("dj1", "textfont", bf, null);
        s.setFieldProperty("zj1", "textfont", bf, null);
        s.setFieldProperty("djhs1", "textfont", bf, null);
        s.setFieldProperty("zjhs1", "textfont", bf, null);

        s.setFieldProperty("mc2", "textfont", bf, null);
        s.setFieldProperty("pp2", "textfont", bf, null);
        s.setFieldProperty("gg2", "textfont", bf, null);
        s.setFieldProperty("sl2", "textfont", bf, null);
        s.setFieldProperty("dj2", "textfont", bf, null);
        s.setFieldProperty("zj2", "textfont", bf, null);
        s.setFieldProperty("djhs2", "textfont", bf, null);
        s.setFieldProperty("zjhs2", "textfont", bf, null);

        s.setFieldProperty("mc3", "textfont", bf, null);
        s.setFieldProperty("pp3", "textfont", bf, null);
        s.setFieldProperty("gg3", "textfont", bf, null);
        s.setFieldProperty("sl3", "textfont", bf, null);
        s.setFieldProperty("dj3", "textfont", bf, null);
        s.setFieldProperty("zj3", "textfont", bf, null);
        s.setFieldProperty("djhs3", "textfont", bf, null);
        s.setFieldProperty("zjhs3", "textfont", bf, null);

        s.setFieldProperty("mc4", "textfont", bf, null);
        s.setFieldProperty("pp4", "textfont", bf, null);
        s.setFieldProperty("gg4", "textfont", bf, null);
        s.setFieldProperty("sl4", "textfont", bf, null);
        s.setFieldProperty("dj4", "textfont", bf, null);
        s.setFieldProperty("zj4", "textfont", bf, null);
        s.setFieldProperty("djhs4", "textfont", bf, null);
        s.setFieldProperty("zjhs4", "textfont", bf, null);

        s.setFieldProperty("mc5", "textfont", bf, null);
        s.setFieldProperty("pp5", "textfont", bf, null);
        s.setFieldProperty("gg5", "textfont", bf, null);
        s.setFieldProperty("sl5", "textfont", bf, null);
        s.setFieldProperty("dj5", "textfont", bf, null);
        s.setFieldProperty("zj5", "textfont", bf, null);
        s.setFieldProperty("djhs5", "textfont", bf, null);
        s.setFieldProperty("zjhs5", "textfont", bf, null);

        s.setFieldProperty("mx1", "textfont", bf, null);
        s.setFieldProperty("mx2", "textfont", bf, null);
        s.setFieldProperty("mx3", "textfont", bf, null);
        s.setFieldProperty("mx4", "textfont", bf, null);
        s.setFieldProperty("mx5", "textfont", bf, null);
        s.setFieldProperty("mx6", "textfont", bf, null);
        s.setFieldProperty("mx7", "textfont", bf, null);
        s.setFieldProperty("mx8", "textfont", bf, null);
        s.setFieldProperty("mx9", "textfont", bf, null);
        s.setFieldProperty("mx10", "textfont", bf, null);





        //设置值（主表）
        s.setField("bm", p.getBm());
        s.setField("sqr", p.getSqr());
        s.setField("sqrq", sdf.format(p.getDate()));
        s.setField("zje", p.getZje());
        s.setField("zjehs", p.getZjehs());
        s.setField("pjlx", p.getPj());
        s.setField("pjlxhs", p.getPhhs());
        s.setField("lxfs", p.getGys());
        s.setField("cgsm", p.getCgsm());
        s.setField("ssbm", p.getCgbm());
        s.setField("ysbm", p.getYsbm());
        s.setField("spzt", p.getStatus());
        List<ApplicationDetil> list=p.getApplicationDetilList();
        for(int i=1;i<=list.size();i++){
            s.setField("mc"+i, list.get(i-1).getMc());
            s.setField("pp"+i, list.get(i-1).getPp());
            s.setField("gg"+i, list.get(i-1).getGg());
            s.setField("sl"+i, list.get(i-1).getSl());
            s.setField("dj"+i, list.get(i-1).getDj());
            s.setField("zj"+i, list.get(i-1).getZj());
            s.setField("djhs"+i, list.get(i-1).getDjhs());
            s.setField("zjhs"+i, list.get(i-1).getZjhs());
        }

        for(int i=1;i<=l.size();i++){
            s.setField("mx"+i, fz(l.get(i-1)));
        }
        ps.setFormFlattening(true); // 这句不能少

        ps.close();
        reader.close();
        fos.close();
    }
    public  String fz(PaymentApproval a){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(a.getReson()==null){
            a.setReson("");
        }
        StringBuffer sb=new StringBuffer();
        sb.append(sf.format(a.getDate())).append("   ").append(a.getPerson()).append("   ").append(a.getAction()).append("   ");
        sb.append("审核意见:").append(a.getReson());
        return sb.toString();
    }
    public  String fz(ApplicationApproval a){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(a.getReson()==null){
            a.setReson("");
        }
        StringBuffer sb=new StringBuffer();
        sb.append(sf.format(a.getDate())).append("   ").append(a.getPerson()).append("   ").append(a.getAction()).append("   ");
        sb.append("审核意见:").append(a.getReson());
        return sb.toString();
    }
    public  static  String getNumber(int n){
        if(n==0){return "零";}
        else if(n==1){return "壹";}
        else if(n==2){return "贰";}
        else if(n==3){return "叁";}
        else if(n==4){return "肆";}
        else if(n==5){return "伍";}
        else if(n==6){return "陆";}
        else if(n==7){return "柒";}
        else if(n==8){return "捌";}
        else {return "玖";}
    }
    /**
     *
     * @param folder       要合并文件的文件夹
     * @param outPdfFile    合并后的文件名
     * @param fileString    需要合并的pdf文件名集合
     * @throws Exception
     */
    public void mergePdf(String folder, String outPdfFile, List<String> fileString) throws Exception {
        PDFMergerUtility mergePdf = new PDFMergerUtility();
        List<String> filesInFolder = fileString;
        for (int i = 0; i < filesInFolder.size(); i++)
            mergePdf.addSource(folder + File.separator + filesInFolder.get(i));
        mergePdf.setDestinationFileName(outPdfFile);
        mergePdf.mergeDocuments();
    }

}
